<template>
    <div class="dialogPop">
      <el-dialog :visible.sync="addData.dialogUpdata" :before-close="handleClose" size="tiny" title="添加学校配置">
        <el-form :model="formInline" :rules="rules" ref="formInline" label-width="90px">
            <el-row :gutter="20">
                <el-col :span="22">
                    <el-form-item label="学校名称" prop="schoolName">
                        <school v-model="formInline.schoolName"  placeholder="请输入学校名称" style="width:100%" @change='schoolChange'></school>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="22">
                <el-form-item label="公网IP:" prop="sPublicNetworkIp">
                    <el-popover placement="top-start" trigger="focus"
                                    content="字符长度不大于20个">
                        <el-input v-model="formInline.sPublicNetworkIp" slot="reference" :maxlength="20"  placeholder="请输入公网IP"></el-input>
                    </el-popover>
                </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="22">
                <el-form-item label="端口号:" prop="sPort">
                    <el-popover placement="top-start" trigger="focus"
                                    content="字符长度不大于5个">
                        <el-input v-model="formInline.sPort" slot="reference" :maxlength="5"  placeholder="请输入端口号"></el-input>
                    </el-popover>
                </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <p class="btn">
            <el-button @click="resetForm('formInline')">取 消</el-button>
            <el-button type="primary" @click="updataBtn('formInline')" :loading="loadingBtn">确 认</el-button>
          </p>
        </div>
      </el-dialog>
    </div>
</template>
<script>

import { addSettlementConfig } from 'api/deviceManagement';
import school from 'components/SchoolClasss/school';
export default {
    components: { school },
    props: {
        addData: {
            default() {
                return {
                    dialogUpdata: false // 默认不显示
                }
            }
            // required: true
        }
    },
    data() {
        return {
            loadingBtn: false,
            formInline: {
                schoolName: '',
                schoolId: '',
                sPublicNetworkIp: '',
                sPort: ''
            },
            rules: {
                schoolName: [
                    { required: true, message: '请输入学校名称', trigger: 'blur' }
                ],
                sPublicNetworkIp: [
                     { required: true, message: '请输入公网IP', trigger: 'blur' },
                     { pattern: /^(?=(\b|\D))(((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))(?=(\b|\D))$/, message: '请输入正确的公网IP', trigger: 'blur' }
                ],
                sPort: [
                     { required: true, message: '请输入端口号', trigger: 'blur' },
                     { pattern: /^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-5]{2}[0-3][0-5])$/, message: '请输入正确的端口号', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        // 确定按钮
        updataBtn(formInline) {
            
            this.$refs[formInline].validate((valid) => {
                if (valid) {// 验证通过
                    this.loadingBtn=true;

                    addSettlementConfig(this.formInline.schoolId, this.formInline.sPublicNetworkIp, this.formInline.sPort).then(res => {
                        if (res.data.status==200) {
                            this.loadingBtn=false;
                            this.$message({
                                type: 'success',
                                message: '添加成功!'
                            });
                            this.$emit('searchList');// 更新列表
                            this.handleClose();
                        }
                    }).catch((res) => {
                        this.loadingBtn=false;
                    })
                } else {
                    return false;
                }
            });
        },
        handleClose() {
            this.delDialogAddTea();
        },
        // 取消按钮 
        resetForm(formInline) {
            // this.$refs[formInline].resetFields();
            this.delDialogAddTea();
        },
        // 获取学校id
        schoolChange(val) {
            this.formInline.schoolId = val;
            console.log('qqq', this.formInline.schoolId);
        },
        // 还原弹窗信息
        delDialogAddTea() {
            this.addData.dialogUpdata = false;
        }
    }
}
</script>



